package com.ning.activitys.skysound.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.ning.activitys.skysound.beans.MusicDetailsBeans;
import com.ning.activitys.skysound.beans.NativeInformationBeans;
import com.ning.activitys.skysound.datas.Constant;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by dllo on 16/1/8.
 *
 *  Activity类 、Service类 、Application类本质上都是Context子类
 */
public class MyApplication extends Application{

    private static Context context;
    private static List<Activity> activities;

    /**
     * 在这里,新建SharePreferences对象,方便全局调用
     * */
    public static SharedPreferences sharedPreferences;

    public static DbUtils dbUtils;
    @Override
    public void onCreate() {
        super.onCreate();

        JPushInterface.init(this);
        JPushInterface.setDebugMode(true);


        MyApplication.context = this;

        sharedPreferences  = getSharedPreferences(Constant.SP_NAME, MODE_APPEND);
        dbUtils = DbUtils.create(getApplicationContext(), Constant.DB_NAME);

        try {
            dbUtils.createTableIfNotExist(MusicDetailsBeans.class);
            dbUtils.createTableIfNotExist(NativeInformationBeans.class);
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    public static Context getContext(){

        return MyApplication.context;
    }

    public static void addActivity(Activity activity){
        if (activities == null){
            activities = new ArrayList<>();
        }
        activities.add(activity);
    }

    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }

    public static void finishAll(){
        for (Activity activity : activities){
            if ( !activity.isFinishing() ){
                activity.finish();
            }
        }
    }
}
